package com.wf.ew.sell.service.impl;

import com.wf.ew.common.JsonResult;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.ArithUtil;
import com.wf.ew.sell.biz.SellBiz;
import com.wf.ew.sell.model.EntryBillSon;
import com.wf.ew.sell.model.OutBillSon;
import com.wf.ew.sell.model.Sell;
import com.wf.ew.storage.biz.ProductStorageBiz;
import com.wf.ew.storage.model.ProductStorage;
import com.wf.ew.system.model.User;
import com.wf.ew.sell.biz.SellSonBiz;
import com.wf.ew.sell.model.SellSon;
import com.wf.ew.sell.service.SellSonService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;

/**
 *
 * 数据服务层接口实现类
 * @author : Doman
 * @date : 2019-12-08 23:22:41
 * @version ：1.0
 */
@Service
public class SellSonServiceImpl implements SellSonService {

	@Autowired
	private SellBiz sellBiz;
	@Autowired
	private SellSonBiz sellSonBiz;
	@Autowired
	private ProductStorageBiz productStorageBiz;

	@Override
	public String list(PageResult<T> page, SellSon sellSon, User loginUser) {
		return sellSonBiz.list(page,sellSon,loginUser);
	}
	
	@Override
	public List<SellSon> list(SellSon sellSon, User loginUser) {
		return sellSonBiz.list(sellSon,loginUser);
	}

	@Override
	public Double calcTotalPrice(SellSon sellSon, User loginUser) {
		if(sellSon == null && sellSon.getSellId().equals("")){
			return 0.0;
		} else {
			double total = 0.0;
			try {
				total = sellSonBiz.calcTotalPrice(sellSon,loginUser);
			} catch (Exception e){
				e.printStackTrace();
			}
			Sell sell = new Sell();
			sell.setId(sellSon.getSellId());
			sell.setTotal(total);
			sellBiz.updateTotal(sell, loginUser);
			return total;
		}

	}

	@Override
	public SellSon getById(SellSon sellSon) {
		return sellSonBiz.getById(sellSon);
	}

	@Transactional
	@Override
	public JsonResult add(SellSon sellSon, User loginUser) {
		if (sellSonBiz.add(sellSon, loginUser)) {
			if(sellSon.getNum()>=0){
				ProductStorage ps = new ProductStorage();
				ps.setWarehouseName(sellSon.getWarehouseName());
				ps.setProductName(sellSon.getProductName());
				ps.setSpecification(sellSon.getSpecification());
				ps.setLengths(sellSon.getLengths());
				ps.setNegative(sellSon.getNegative());
				ps.setNum(sellSon.getNum());
				ps.setLiWeight(sellSon.getLiWeight());
				ps.setLiTotalWeight(Double.valueOf(sellSon.getLiTotalWeight()));
				ps.setPrice(sellSon.getPrice());
				ps.setModel(sellSon.getModel());
				ps.setLocality(sellSon.getLocality());
				List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
				if (psList != null && psList.size()>0) {
					ProductStorage ps2 = psList.get(0);
					ps2.setNum(ArithUtil.sub(0,sellSon.getNum()));
					ps2.setLiTotalWeight(ArithUtil.sub(0,Double.valueOf(sellSon.getLiTotalWeight())));
					productStorageBiz.updateNum(ps2, loginUser);
				} else {
					ps.setNum(ArithUtil.sub(0,sellSon.getNum()));
					productStorageBiz.add(ps, loginUser);
				}
			}
			return JsonResult.ok("添加成功");
		}
		return JsonResult.error("添加失败");
	}

	@Transactional
	@Override
	public JsonResult update(SellSon sellSon, User loginUser) {
		SellSon b2 = sellSonBiz.getById(sellSon);
		if (sellSonBiz.update(sellSon, loginUser)) {
			if(sellSon.getNum()>=0){
				ProductStorage ps = new ProductStorage();
				ps.setWarehouseName(sellSon.getWarehouseName());
				ps.setProductName(sellSon.getProductName());
				ps.setSpecification(sellSon.getSpecification());
				ps.setLengths(sellSon.getLengths());
				ps.setNegative(sellSon.getNegative());
				List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
				if (psList != null && psList.size()>0) {
					ProductStorage ps2 = psList.get(0);
					ps2.setNum(ArithUtil.sub(0,ArithUtil.sub(sellSon.getNum(),b2.getNum())));
					ps2.setLiTotalWeight(ArithUtil.sub(0,
							ArithUtil.sub(Double.valueOf(sellSon.getLiTotalWeight()),
									Double.valueOf(b2.getLiTotalWeight()))));
					productStorageBiz.updateNum(ps2, loginUser);
				} else {
					ps.setNum(ArithUtil.sub(0,sellSon.getNum()));
					productStorageBiz.add(ps, loginUser);
				}
			}
			return JsonResult.ok("修改成功");
		}
		return JsonResult.error("修改失败");
	}

	@Transactional
	@Override
	public JsonResult delete(SellSon sellSon, User loginUser) {
		sellSon.setId(sellSon.getIds());
		SellSon b2 = sellSonBiz.getById(sellSon);
		if (sellSonBiz.delete(sellSon, loginUser)) {
			ProductStorage ps = new ProductStorage();
			ps.setWarehouseName(b2.getWarehouseName());
			ps.setProductName(b2.getProductName());
			ps.setSpecification(b2.getSpecification());
			ps.setLengths(b2.getLengths());
			ps.setNegative(b2.getNegative());
			List<ProductStorage> psList = productStorageBiz.entity(ps, loginUser);
			if (psList != null && psList.size()>0) {
				ProductStorage ps2 = psList.get(0);
				ps2.setNum(b2.getNum());
				ps2.setLiTotalWeight(Double.valueOf(b2.getLiTotalWeight()));
				productStorageBiz.updateNum(ps2, loginUser);
			}
			return JsonResult.ok("删除成功");
		}
		return JsonResult.error("删除失败");
	}
}
